package com.blockchain.console.servlets.content.gggl;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.common.enums.IsPass;
import com.blockchain.common.enums.NoticeType;
import com.blockchain.console.service.content.NoticeManage;
import com.blockchain.console.service.content.entity.Notice;
import com.blockchain.framework.http.servlet.annotation.Right;
import com.blockchain.framework.http.upload.FileStore;
import com.blockchain.framework.resource.PromptLevel;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.framework.service.exception.LogicalException;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.util.parser.EnumParser;
import com.blockchain.util.parser.TimestampParser;

@Right(id = "C_CONTENT_GGGL_ADD", name = "公告管理-增加")
public class AddGggl extends AbstractGgglServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(final HttpServletRequest request,
			HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		try {
			NoticeManage manage = serviceSession.getService(NoticeManage.class);

			Notice notice = new Notice() {

				@Override
				public NoticeType getType() {
					return EnumParser.parse(NoticeType.class,
							request.getParameter("type"));
				}

				@Override
				public String getTitle() {
					return request.getParameter("title");
				}

				@Override
				public IsPass getPublishStatus() {
					return EnumParser.parse(IsPass.class,
							request.getParameter("publishStatus"));
				}

				@Override
				public String getContent() {
					return getResourceProvider().getResource(FileStore.class)
							.encode(request.getParameter("content"));
				}

				@Override
				public Timestamp getCreateTime() {
					return TimestampParser.parse(request.getParameter("createTime"));
				}

				@Override
				public String getTitleEn() {
					return request.getParameter("titleEn");
				}

				@Override
				public String getTitleTw() {
//					return request.getParameter("titleTw");
					return "test";
				}

				@Override
				public String getContentEn() {
					return getResourceProvider().getResource(FileStore.class)
							.encode(request.getParameter("contentEn"));
				}

				@Override
				public String getContentTw() {
//					return getResourceProvider().getResource(FileStore.class)
//							.encode(request.getParameter("contentTw"));
					return "test";
				}
			};
			manage.add(notice);
			sendRedirect(request, response,
					getController().getURI(request, SearchGggl.class));
		} catch (ParameterException | LogicalException e) {
			prompt(request, response, PromptLevel.ERROR, e.getMessage());
			processGet(request, response, serviceSession);
		}
	}

}
